package com.edroid.apkhooker.home;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.edroid.apkhooker.R;
import com.example.service.SystemServiceGetter;

public class AppListAdapter extends ArrayAdapter<AppEntry> implements OnCheckedChangeListener {
	private final LayoutInflater mInflater;

	
	public AppListAdapter(Context context) {
		super(context, android.R.layout.simple_list_item_2);
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public void setData(List<AppEntry> data) {
		clear();
		if (data != null) {
			addAll(data);
		}
	}

	/**
	 * Populate new items in the list.
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view;
		ViewHolder holder;

		if (convertView == null) {
			view = mInflater.inflate(R.layout.list_item, parent, false);
			holder = new ViewHolder();
			view.setTag(holder);
			
			holder.imageView = ((ImageView) view.findViewById(R.id.imageView1));
			holder.textView1 =  ((TextView) view.findViewById(R.id.textView1));
			holder.textView2 =  ((TextView) view.findViewById(R.id.textView2));
//			holder.checkBox1 =  ((CheckBox) view.findViewById(R.id.checkBox1));
//			holder.checkBox1.setOnCheckedChangeListener(this);
		} else {
			view = convertView;
			holder = (ViewHolder) convertView.getTag();
		}

		AppEntry item = getItem(position);
		holder.imageView.setImageDrawable(item.getIcon());
		holder.textView1.setText(item.getLabel());
		holder.textView2.setText(item.getApplicationInfo().packageName);
//		holder.checkBox1.setTag(position);
//		holder.checkBox1.setChecked(item.mChecked);
		
		try {
			boolean b = SystemServiceGetter.getSystemService().isWatched(item.getPackgeName());
			holder.textView1.setTextColor(b? Color.RED : Color.BLACK);
		} catch (Exception e) {
		}

		return view;
	}
	
	class ViewHolder {
		ImageView imageView;
		TextView textView1, textView2;
		CheckBox checkBox1;
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		getItem((Integer)buttonView.getTag()).mChecked = isChecked;
	}
}
